#include "compositeviewerexp.h"
#include "viewer.h"
#include "scene.h"

namespace CoreBase
{
	CompositeViewerExp::CompositeViewerExp():_eventEnable(true)
	{}

	CompositeViewerExp::~CompositeViewerExp()
	{}

	void CompositeViewerExp::frame(double simulationTime)
	{
		if (_done) return;
		if (_firstFrame)
		{
			viewerInit();
			if (!isRealized())
			{
				realize();
			}
			_firstFrame = false;
		}
		advance(simulationTime);
		if(_eventEnable)
			eventTraversal();
		testViewChange();
		for(int i=0;i<_viewerVector.size();++i)
		{
			if(_viewerVector.at(i)->isSetupScene())
			{
				_viewerVector.at(i)->getScene()->postEventTraversal(simulationTime);
				_viewerVector.at(i)->getScene()->prePhysicsStepUpdate(simulationTime);
			}
		}
		updateTraversal();
		renderingTraversals();
		for(int i=0;i<_viewerVector.size();++i)
		{
			if(_viewerVector.at(i)->isSetupScene())
			{
				_viewerVector.at(i)->getScene()->frameStepUpdate(simulationTime);
				_viewerVector.at(i)->getScene()->postPhysicsStepUpdate(simulationTime);
			}
		}
	}

	void CompositeViewerExp::eventEnable(bool b)
	{
		_eventEnable = b;
	}

	void CompositeViewerExp::testViewChange()
	{
		if(_viewerVector.size()!=_views.size())
		{
			_viewerVector.clear();
			for(int i=0;i<_views.size();++i)
			{
				_viewerVector.push_back(static_cast<CoreBase::Viewer*>(_views.at(i).get()));
			}
		}
	}
}